package org.domon.manager.sys.controller;

import java.util.Collections;
import java.util.List;

import org.domon.manager.base.handler.BaseController;
import org.domon.manager.common.WebConstants;
import org.domon.manager.model.ResultObject;
import org.domon.manager.sys.model.Menu;
import org.domon.manager.sys.model.Role;
import org.domon.manager.sys.model.UserInfo;
import org.domon.manager.sys.service.SysMenuService;
import org.domon.manager.sys.service.SysRoleService;
import org.domon.manager.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys")
public class SystemController extends BaseController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysMenuService sysMenuService;

	@Autowired
	private SysRoleService sysRoleService;

	/**
	 * 菜单管理页面
	 * 
	 * @param userInfo
	 * @param model
	 * @return
	 */
	@RequestMapping("/menu")
	public String menu(UserInfo userInfo, Model model) {
		// 根菜单ID
		model.addAttribute("baseMenuId", WebConstants.BASE_MENU_ID);
		return "system/menu";
	}

	/**
	 * 菜单查询
	 * 
	 * @param userInfo
	 * @param menu
	 * @param searchSubmenu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menu/query")
	public List<?> menuQuery(UserInfo userInfo, Menu menu, @RequestParam(defaultValue = "false") boolean searchSubmenu) {
		List<Menu> menuList = Collections.emptyList();

		if (searchSubmenu) {
			menuList = sysMenuService.queryAll(menu);
		} else {
			menuList = sysMenuService.query(menu);
		}

		return menuList;
	}

	/**
	 * 用户菜单查询
	 * 
	 * @param userInfo
	 * @param userId
	 *            用户ID
	 * @param menuId
	 *            父菜单ID，默认根菜单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menu/query/user")
	public Menu menuQueryByUser(UserInfo userInfo, @RequestParam(required = true) Integer userId) {

		Menu menu = sysMenuService.queryAllUserMenu(userId, WebConstants.BASE_MENU_ID);
		return menu;
	}

	/**
	 * 角色菜单查询
	 * 
	 * @param userInfo
	 * @param roleId
	 *            角色ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menu/query/role")
	public Menu menuQueryByRole(UserInfo userInfo, @RequestParam(required = true) Integer roleId) {
		Menu menu = sysMenuService.queryAllRoleMenu(roleId, WebConstants.BASE_MENU_ID);
		return menu;
	}

	/**
	 * 保存菜单
	 * 
	 * @param userInfo
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menu/save")
	public ResultObject<?> menuSave(UserInfo userInfo, Menu menu) {
		ResultObject<Menu> resultObject = new ResultObject<Menu>();
		try {
			menu = sysMenuService.save(menu, userInfo);
			logger.info("用户[{}]保存菜单[id={}][name={}]成功", userInfo.getUserName(), menu.getMenuId(), menu.getMenuName());
			resultObject.setSuccess(true);
			resultObject.setMsg("保存成功！");
			resultObject.setResult(menu);
		} catch (Exception e) {
			logger.error("用户[{}]保存菜单[id={}][name={}]失败", userInfo.getUserName(), menu.getMenuId(), menu.getMenuName(), e);
			resultObject.setSuccess(false);
			resultObject.setMsg("保存失败！");
		}
		return resultObject;
	}

	/**
	 * 删除菜单
	 * 
	 * @param userInfo
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/menu/remove")
	public ResultObject<?> menuRemove(UserInfo userInfo, Menu menu) {
		ResultObject<Menu> resultObject = new ResultObject<Menu>();
		try {
			sysMenuService.remove(menu.getMenuId());
			logger.info("用户[{}]删除菜单[id={}]成功", userInfo.getUserName(), menu.getMenuId());
			resultObject.setSuccess(true);
			resultObject.setMsg("删除成功！");
			resultObject.setResult(menu);
		} catch (Exception e) {
			logger.error("用户[{}]删除菜单[id={}]失败", userInfo.getUserName(), menu.getMenuId(), e);
			resultObject.setSuccess(false);
			resultObject.setMsg("删除失败！");
		}
		return resultObject;
	}

	/**
	 * 角色管理页面
	 * 
	 * @param userInfo
	 * @param model
	 * @return
	 */
	@RequestMapping("/role")
	public String role(UserInfo userInfo, Model model) {
		// 根菜单ID
		model.addAttribute("baseMenuId", WebConstants.BASE_MENU_ID);
		return "system/role";
	}

	/**
	 * 角色查询
	 * 
	 * @param userInfo
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/query")
	public List<?> roleQuery(UserInfo userInfo, Role role) {
		List<Role> menuList = sysRoleService.query(role);
		return menuList;
	}

	/**
	 * 查询拥有该菜单的角色
	 * 
	 * @param userInfo
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/query/menu")
	public List<?> roleQueryMenu(UserInfo userInfo, Integer menuId) {
		Menu menu = new Menu();
		menu.setMenuId(menuId);
		List<Role> menuList = sysRoleService.queryByMenu(menu);
		return menuList;
	}

	/**
	 * 保存角色
	 * 
	 * @param userInfo
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/save")
	public ResultObject<?> roleSave(UserInfo userInfo, Role role) {
		ResultObject<Role> resultObject = new ResultObject<Role>();
		try {
			role = sysRoleService.save(role, userInfo);
			logger.info("用户[{}]保存菜单[id={}][name={}]成功", userInfo.getUserName(), role.getRoleId(), role.getRoleName());
			resultObject.setSuccess(true);
			resultObject.setMsg("保存成功！");
			resultObject.setResult(role);
		} catch (Exception e) {
			logger.error("用户[{}]保存菜单[id={}][name={}]失败", userInfo.getUserName(), role.getRoleId(), role.getRoleName(), e);
			resultObject.setSuccess(false);
			resultObject.setMsg("保存失败！");
		}
		return resultObject;
	}

	/**
	 * 删除菜单
	 * 
	 * @param userInfo
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/remove")
	public ResultObject<?> roleRemove(UserInfo userInfo, Role role) {
		ResultObject<Role> resultObject = new ResultObject<Role>();
		try {
			sysRoleService.remove(role.getRoleId());
			logger.info("用户[{}]删除菜单[id={}]成功", userInfo.getUserName(), role.getRoleId());
			resultObject.setSuccess(true);
			resultObject.setMsg("删除成功！");
			resultObject.setResult(role);
		} catch (Exception e) {
			logger.error("用户[{}]删除菜单[id={}]失败", userInfo.getUserName(), role.getRoleId(), e);
			resultObject.setSuccess(false);
			resultObject.setMsg("删除失败！");
		}
		return resultObject;
	}

	/**
	 * 保存角色的菜单资源
	 * 
	 * @param userInfo
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/save/menu")
	public ResultObject<?> roleSaveMenu(UserInfo userInfo, Integer roleId, @RequestParam("menuIds[]") int[] menuIds) {
		ResultObject<Role> resultObject = new ResultObject<Role>();
		try {
			sysRoleService.saveMenu(roleId, menuIds, userInfo);
			logger.info("用户[{}]配置角色[id={}]菜单资源成功", userInfo.getUserName(), roleId);
			resultObject.setSuccess(true);
			resultObject.setMsg("配置成功！");
		} catch (Exception e) {
			logger.error("用户[{}]配置角色[id={}]菜单资源失败", userInfo.getUserName(), roleId, e);
			resultObject.setSuccess(false);
			resultObject.setMsg("保存失败！");
		}
		return resultObject;
	}

	@InitBinder("user")
	public void initBinder2(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("user.");
	}

	/**
	 * 角色管理页面
	 * 
	 * @param userInfo
	 * @param model
	 * @return
	 */
	@RequestMapping("/user")
	public String user(UserInfo userInfo, Model model) {
		// 根菜单ID
		model.addAttribute("baseMenuId", WebConstants.BASE_MENU_ID);
		return "system/user";
	}
}
